import axios from 'axios';
// import utils from "../utils/index";
// import store from "@/store";

import { createDiscreteApi, darkTheme, lightTheme } from "naive-ui";

//还有：notification, dialog, loadingBar, modal 
const theme = "light"
const { message } = createDiscreteApi(["message"], {
    configProviderProps: {
        theme: theme === "light" ? lightTheme : darkTheme
    }
});

// const axios = Axios.create();
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'; 

// axios.cancel = {};
axios.interceptors.request.use((config) => {
    // config.headers['Authorization'] = "Bearer " + store.state.user.token;
    if (config.method === 'post') { }
    if (config.method === 'get') { }
    return config;
});


axios.interceptors.response.use(
    (response) => {
        // console.log(567,response.data.code);
        if (response.data.code != 200) {
            message.error(response.data.message)
            if (window.onResError) window.onResError()
        }
        return response
    },
    (error) => {
        // console.log(123,error); 
        message.error("请求失败，请重试！")
        if (window.onResError) window.onResError()
        return Promise.reject({
            message: error.message,
            code: error.code
        })
    }
)

axios.defaults.baseURL = import.meta.env.VITE_APP_BASE_API;
export default axios;